package com.client.ui;

import java.util.Collection;
import java.util.Scanner;

import com.bean.Message;
import com.bean.Product;
import com.bean.User;
import com.client.biz.ProductBiz;
import com.client.biz.UserBiz;
import com.client.biz.product.ProductBizImpl;
import com.client.biz.user.UserBizImpl;
import com.client.net.ResponseCode;

public class HomeScreen {
	
	private Scanner scanner;
	private ProductBiz biz;

	public HomeScreen() {
		scanner = new Scanner(System.in);
		biz = new ProductBizImpl();
	}

	/**
	 * 主界面现实
	 */
	public void showMenu() {
		while(true){
			UIHelper.show("**********欢迎使用校园超市管理系统**********");
			UIHelper.show("\t\t1.登录\n\t\t2.退出");
			UIHelper.show("**************************************");
			UIHelper.show("请选择数字：");
			String num = scanner.next();
			if (num.equals("1")) {
				login();
				break;
			} else if (num.equals("2")) {
				UIHelper.show("您已成功退出系统!");
				break;
			} else {
				UIHelper.show("输入有误，请选择1-2");
			}
		}
	}
	
	/**
	 * 管理员管理界面
	 */
	private void adminManager() {
		UIHelper.show("********欢迎登录校园超市库存管理系统*********");
		int switchNum = 0;
		do {
			UIHelper.show("请选择进行的操作：1.商品入库\t2.商品出库\t3.新增商品\t4.查询全部商品\t5.按编号查询商品\t6.退出");
			switchNum = scanner.nextInt();
			switch(switchNum) {
				case 1:
					updataQuantity("/product/putProduct.do");
					break;
				case 2:
					updataQuantity("/product/removeProduct.do");
					break;
				case 3:
					newProduct();
					break;
				case 4:
					findAllProduct();
					break;
				case 5:
					findProductById();
					break;
				case 6:
					UIHelper.show("成功退出!!!");
					break;
				default:
					UIHelper.show("输入有误，请选择1-6");
			}
		} while(switchNum != 6);
	}
	
	/**
	 * 收银员管理界面
	 */
	private void cashierManager() {
		UIHelper.show("******************欢迎登录校园超市收银管理系统******************");
		int switchNum = 0;
		do {
			UIHelper.show("请选择进行的操作：1.扫描商品\t2.修改数量\t3.结账\t4.退出");
			switchNum = scanner.nextInt();
			switch(switchNum) {
				case 1:
					scanProduct();
					break;
				case 2:
					updataProduct();
					break;
				case 3:
					settleAccount();
					break;
				case 4:
					break;
				default:
					UIHelper.show("输入有误，请选择1-4");
			}
		} while (switchNum != 4);
	}

	/**
	 * 结账
	 */
	private void settleAccount() {
		
	}

	/**
	 * 修改商品数量
	 */
	private void updataProduct() {
		
	}

	/**
	 * 扫描商品
	 */
	private void scanProduct() {
		
	}

	/**
	 * 根据编号查找商品
	 */
	private void findProductById() {
		Product product = new Product();
		UIHelper.show("请输入商品编号：");
		product.setId(scanner.next());
		Message message = biz.findById(product);
		UIHelper.show(message.getRespDes());
		UIHelper.show(message.getProduct());
	}

	/**
	 * 查看所有商品
	 */
	private void findAllProduct() {
		Message message = biz.findAll();
		UIHelper.show(message.getRespDes());
		UIHelper.showLine();
		showProductList(message.getAllProducts());
	}

	/**
	 * 新增商品
	 */
	private void newProduct() {
		Product product = new Product();
		UIHelper.show("请输入商品编号：");
		product.setId(scanner.next());
		UIHelper.show("请输入商品名称：");
		product.setName(scanner.next());
		UIHelper.show("请输入商品入库数量：");
		product.setQuantity(scanner.next());
		UIHelper.show("请输入商品价格：");
		product.setPrice(scanner.next());
		UIHelper.show("请输入商品单位：");
		product.setCompany(scanner.next());
		Message message = biz.newProduct(product);
		UIHelper.show(message.getRespDes());
	}

	/**
	 * "/product/putProduct.do"
	 * 修改商品数量
	 */
	private void updataQuantity(String url) {
		UIHelper.show("请输入入库商品编号：");
		String id = scanner.next();
		UIHelper.show("请输入入库的数量：");
		String quantity = scanner.next();
		Product product = new Product();
		product.setId(id);
		product.setQuantity(quantity);
		Message message = biz.updataQuantity(url, product);
		UIHelper.show(message.getRespDes());
		UIHelper.show(message.getProduct());
	}
	
	private void login() {
		UserBiz biz = new UserBizImpl();
		boolean isSkip = false;
		do {
			UIHelper.show("用户名：");
			String username = scanner.next();
			UIHelper.show("密码：");
			String password = scanner.next();
			User user = new User(username, password);
			Message resp = biz.login(user);
			int respCode = Integer.valueOf(resp.getUser().getResponsType());
			switch(respCode) {
				case ResponseCode.RESP_ERROE:
					UIHelper.show(resp.getRespDes());
					break;
				case ResponseCode.LOGIN_ADMIN:
					adminManager();
					isSkip = true;
					break;
				case ResponseCode.LOGIN_CASHIER:
					cashierManager();
					isSkip = true;
					break;
			}
		} while (!isSkip);
	}
	
	private void showProductList(Collection<Product> products) {
		UIHelper.show("商品编号\t\t|\t商品名称\t\t|\t商品价格\t\t|\t商品单位\t\t|\t商品数量");
		StringBuffer buffer = new StringBuffer();
		for (Product p : products) {
			buffer.append(p.getId());
			buffer.append("\t\t|\t");
			buffer.append(p.getName());
			buffer.append("\t\t|\t");
			buffer.append(p.getPrice());
			buffer.append("\t\t|\t");
			buffer.append(p.getCompany());
			buffer.append("\t\t|\t");
			buffer.append(p.getQuantity());
			buffer.append("\n");
			buffer.append(UIHelper.LINE).append("\n");
		}
		System.out.println(buffer);
	}
}
